/**
 * 财富热度
 * @author anpt
 */
define(["require",
		"dojo/_base/declare",
		"dojo/_base/lang",
		"dojo/_base/array",
		"dojo/_base/fx",
		"dojo/fx/easing",
		"../../util/ajaxUtil",
		"dojo/dom",
		"dojo/dom-attr",
		"dojo/dom-class",
		"dojo/dom-style",
		"dojo/on",
		"dojo/topic",
		"dijit/_WidgetBase",
		"esri/geometry/Polygon",
		"esri/graphic",
		"esri/SpatialReference",
		"esri/InfoTemplate",
		"esri/Color",
		"esri/renderers/ClassBreaksRenderer",
		"esri/symbols/SimpleFillSymbol",
		"esri/symbols/SimpleLineSymbol",
		"esri/layers/GraphicsLayer"],
    function (require,declare,lang,array,baseFX,esring,ajaxUtil,dom,domAttr,domClass,
    		domStyle,on,topic,BaseWidget,Polygon,Graphic,SpatialReference,InfoTemplate,Color,ClassBreaksRenderer,SimpleFillSymbol,SimpleLineSymbol,GraphicsLayer) {
        return declare([BaseWidget], {
        	map:null,
        	extent:null,
        	url:null,
        	condition:null,
        	handles:[],
        	graphicsLayer:null,
        	constructor:function (args) {
        		debugger
                this.map = args.map;
        		this.url="http://api.geoq.cn/a7a6ec3e1a26fe54f369603fdcd5e4369a95e295f60dcfb9a4f69cce3deaceeb/filterservice/regionfilter";
//        		this.url="http://api.geoq.cn/83c11f4076d483549ca9f0d5f6aaeba1d26e969255e80bd0a4f69cce3deaceeb/filterservice/regionfilter";
                this.condition='{"estate_avg_price":[0,1000000]}';
                this.graphicsLayer=new GraphicsLayer({
                	id:"CaiFuLayer",
                	opacity:"0.8",
                	visible:true
                });
                //存储所有graphic图层
        		ioc.map.graphiclayers.push(this.graphicsLayer);
        		this.init();
            },
            init:function(){
                this.initQuery();
            },
            postCreate:function(){
            	this.inherited(arguments);
            	this.handles.push(on(this.map,"extent-change",lang.hitch(this,this.initQuery)));		
            },
            startup:function(){
            	this.inherited(arguments);
            },
            initQuery:function(layer){
            	var map=this.map;
            	var maplevel=map.getLevel();//5,6,7,8,9,10,11,12,13
            	var radius=[250,500,500,1000,1000,2000,2000,4000,4000];
            	var radiu=radius[8-(maplevel-5)];
            	var layer=this.graphicsLayer;
            	layer.clear();
            	var extentobj=this.map.extent;
            	var extent="[" + extentobj.xmin + ","+ extentobj.ymin + "," + extentobj.xmax+ "," + extentobj.ymax + "]";
            	var url=this.url+"?&extent=" + extent+"&condition="+ this.condition+"&grid=square"+"&radius="+radiu;
            	$.ajax({
            		type:"GET",
            		url:url,
            		success:function(result){
            			var features=result.result.features;
            			dojo.forEach(features,lang.hitch(this,function(feature){
            				var polygonJson={"rings":feature.geometry.rings,"spatialReference":{"wkid":102100}}
            				var geometry=new Polygon(polygonJson);
							var graphic=new Graphic({
								"geometry":geometry,
								"attributes":feature.attributes
							});
            				layer.add(graphic);
            			}))
            			var symbol = new SimpleFillSymbol();
                              symbol.setColor(new Color([250, 250, 250, 0.5]));
                        var simpleLine=new SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new Color([255,255,255,0.5]), 1)
            			var renderer = new ClassBreaksRenderer(symbol, "estate_avg_price");
            				 renderer.addBreak(1000, 10000, new SimpleFillSymbol().setColor(new Color([0,102,153, 0.1])).setOutline(simpleLine));
            			     renderer.addBreak(10000, 25000, new SimpleFillSymbol().setColor(new Color([0,102,153, 0.3])).setOutline(simpleLine));
            			     renderer.addBreak(25000, 40000, new SimpleFillSymbol().setColor(new Color([0,102,153, 0.5])).setOutline(simpleLine));
            			     renderer.addBreak(40000, 50000, new SimpleFillSymbol().setColor(new Color([0,102,153, 0.7])).setOutline(simpleLine));
            			     renderer.addBreak(50000, 80000, new SimpleFillSymbol().setColor(new Color([0,102,153,0.8])).setOutline(simpleLine));
            			     renderer.addBreak(80000,150000, new SimpleFillSymbol().setColor(new Color([0,102,153,0.8])).setOutline(simpleLine));
            			     renderer.addBreak(150000,250000, new SimpleFillSymbol().setColor(new Color([0,0,153,0.8])).setOutline(simpleLine));
            			var template=new InfoTemplate({
             				title:"该区域房价",
             				content:"<span>每平米均价:</span><p>${estate_avg_price}元</p>"
             			});
            			layer.setInfoTemplate(template);
            			layer.setRenderer(renderer);
            			map.addLayer(layer,30);
            		}
            	})
            },
            destroy:function(){
    			this.inherited(arguments);
    			this.map = null;
    			dojo.forEach(this.handles,function(hand){
    				hand.remove();
    			})
    		}
        });
    });